﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// Contains the basic information about a project member.
    /// </summary>
    public class Member : DomainBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Specifies the assigned project member.
        /// </summary>
        public ICollection<Team> AssignedTeams
        {
            get
            {
                return DataObject.GetData(
                    Schema.MemberSchema.TeamIds,
                    data =>
                        {
                            IList<Team> teams = new List<Team>();
                            MultiValueChain<string> valueChains = (data as MultiValueChain<string>);
                            if (valueChains != null)
                            {
                                foreach (var valueChain in valueChains.MultiChain)
                                {
                                    if (ParentProject.Teams.ContainsId(valueChain.Top))
                                    {
                                        teams.Add(ParentProject.Teams[valueChain.Top]);
                                    }
                                }
                            }
                            return teams;
                        });
            }
        }

        /// <summary>
        /// Gets the name of the current member object. In most cases this
        /// property will contain the domain object title.
        /// </summary>
        public override string Name
        {
            get { return DataObject.GetData(Schema.MemberSchema.Name, string.Empty); }
            set { }
        }

        /// <summary>
        /// Gets the e-mail of a member.
        /// </summary>
        public string EMail
        {
            get { return DataObject.GetData(Schema.MemberSchema.EMail, string.Empty); }
        }

        /// <summary>
        /// Gets the member picture.
        /// </summary>
        public Uri PictureUri
        {
            get { return DataObject.GetData<Uri>(Schema.MemberSchema.PictureUri); }
        }

        /// <summary>
        /// Returns true if the current user can be used to login [call ChangeUser() ].
        /// </summary>
        public bool CanLogIn
        {
            get { return DataObject.GetData(Schema.MemberSchema.HasLoginData, false); }
        }

        /// <summary>
        /// Gets the parent collection instance.
        /// </summary>
        internal override IDomainCollection ParentCollection
        {
            get { return Parent.SearchCollection<Member>(); }
        }

        /// <summary>
        /// Gets a collection which contains all child collections of the current
        /// domain object.
        /// </summary>
        internal override ICollection ChildCollections { get { return new IDomainCollection[0]; } }

        /// <summary>
        /// Gets the domain object schema of the current domain object.
        /// </summary>
        protected override SchemaBase DomainSchema { get { return Schema.MemberSchema; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Resets all child collections (IDomainCollection instances) of the
        /// current domain object.
        /// </summary>
        protected override void ResetChildCollections()
        {
        }

        /// <summary>
        /// Initializes the mappings stored in the current instance.
        /// </summary>
        protected override void InitializeMappings()
        {
            base.InitializeMappings();

            PropertySchemaMappings.Add(Schema.MemberSchema.Name, "Name");
            PropertySchemaMappings.Add(Schema.MemberSchema.TeamIds, "AssignedTeams");
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}